﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class CaixasUC
    Dim objBLL As New CaixaBLL
#Region "Voltar"
    Private Sub TSBVoltar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub
#End Region
#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.buscar("", 5)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "KEY PRESS"
    Private Sub CBConsultas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = True
    End Sub
#End Region
#Region "ABRIR"
    Private Sub TSBAbrir_Click(sender As System.Object, e As System.EventArgs) Handles TSBAbrir.Click
        TSBAbrir.BackColor = Color.Gainsboro
        TSBAbrir.BackColor = Color.Transparent
        Dim ret As Boolean
        Dim indice As Integer = -1
        Try
            ret = objBLL.validaAbertura
            If ret = True Then
                If DGVDados.Rows.Count < 0 Then
                    For Each linha As DataGridViewRow In DGVDados.SelectedRows
                        If (Not linha.Cells("cai_codigo") Is Nothing) Then
                            indice = linha.Index
                        End If
                    Next
                    If (indice <> -1) Then
                        If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) <> 6) Then
                            CaixaFRM.codigo = Val(DGVDados.Rows(indice).Cells("cai_codigo").Value)
                            TSBAbrir.BackColor = Color.Gainsboro
                            If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) = 4) Then
                                Dim obj As New CaixaFRM()
                                obj.Op = "O"  'efetuar operações
                                obj.codigo = Val(DGVDados.Rows(indice).Cells("cai_codigo").Value)
                                obj.ShowDialog()
                                CarregaGrid()
                                TSBAbrir.BackColor = Color.Gainsboro
                            End If
                        Else
                            If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) = 6) Then
                                Dim obj As New CaixaFRM()
                                obj.Op = "A" 'abrir novo
                                obj.ShowDialog()
                                obj.Limpar()
                                CarregaGrid()
                                TSBAbrir.BackColor = Color.Gainsboro
                            End If
                            'MsgBox("Caixa Fechado. Abra um novo caixa.", MsgBoxStyle.OkCancel)
                        End If
                    Else
                        MessageBox.Show("Selecione o caixa!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Else
                    MsgBox("Existe um caixa aberto!", MsgBoxStyle.Exclamation)
                End If
            Else
                Dim obj As New CaixaFRM()
                obj.Op = "A" 'abrir novo
                obj.ShowDialog()
                obj.Limpar()
                CarregaGrid()
                TSBAbrir.BackColor = Color.Gainsboro
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAbrir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "LOAD"
    Private Sub CaixasUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        '  DGVDados.SelectionMode = False (permite q a linha não fique selecionado
        DGVDados.ReadOnly = True
    End Sub
#End Region
#Region "LOCALIZAR"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PNFiltros.Visible = True Then
            PNFiltros.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PNFiltros.Visible = True
            TXTLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "CBSTATUS"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region
#Region "CBFILTROS"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 2 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            TXTLocalizar.Focus()
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 1 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""
    End Sub
#End Region
#Region "BUSCAR"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            DGVDados.DataSource = objBLL.locdata(DTPInicial.Text, DTPFinal.Text)
            DGVDados.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region
#Region "PROPRIEDADES"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 1 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub CBStatus_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub

    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "MOVIMENTAÇÃO"
    Private Sub TSBMovimentacao_Click(sender As System.Object, e As System.EventArgs) Handles TSBMovimentacao.Click
        'TSBAbrir_Click(Nothing, Nothing)
        TSBAbrir.BackColor = Color.Gainsboro
        TSBAbrir.BackColor = Color.Transparent
        Dim ret As Boolean
        Dim indice As Integer = -1
        Try
            ret = objBLL.validaAbertura
            If ret = True Then
                If DGVDados.Rows.Count <> 0 Then
                    For Each linha As DataGridViewRow In DGVDados.SelectedRows
                        If (Not linha.Cells("cai_codigo") Is Nothing) Then
                            indice = linha.Index
                        End If
                    Next
                    If (indice <> -1) Then
                        If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) <> 6) Then
                            CaixaFRM.codigo = Val(DGVDados.Rows(indice).Cells("cai_codigo").Value)
                            TSBAbrir.BackColor = Color.Gainsboro
                            If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) = 4) Then
                                Dim obj As New CaixaFRM()
                                obj.Op = "O"  'efetuar operações
                                obj.codigo = Val(DGVDados.Rows(indice).Cells("cai_codigo").Value)
                                obj.ShowDialog()
                                CarregaGrid()
                                TSBAbrir.BackColor = Color.Gainsboro
                            End If
                        Else
                            If (Val(DGVDados.Rows(indice).Cells("sta_codigo").Value) = 6) Then
                                Dim obj As New CaixaFRM()
                                obj.Op = "A" 'abrir novo
                                obj.ShowDialog()
                                obj.Limpar()
                                CarregaGrid()
                                TSBAbrir.BackColor = Color.Gainsboro
                            End If
                            'MsgBox("Caixa Fechado. Abra um novo caixa.", MsgBoxStyle.OkCancel)
                        End If
                    Else
                        MessageBox.Show("Selecione o caixa!", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                Else
                    MsgBox("Não existe nenhum caixa aberto!", MsgBoxStyle.Exclamation)
                End If
            Else
                Dim obj As New CaixaFRM()
                obj.Op = "A" 'abrir novo
                obj.ShowDialog()
                obj.Limpar()
                CarregaGrid()
                TSBAbrir.BackColor = Color.Gainsboro
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAbrir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "TXTLOCALIZAR"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Nome"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Data Abertura"
                BBuscar_Click(Nothing, Nothing)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 4)
        End If
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "KEYDOWN"
    Private Sub DGVDados_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DGVDados.KeyDown
        e.SuppressKeyPress = True 'não permite pular linha
    End Sub
#End Region
#Region "Relatórios"
    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("cai_codigo") Is Nothing) Then
                objBLL = New CaixaBLL(Val(DGVDados.Rows(linha.Index).Cells("cai_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.caicodigo > 0) Then gerarelatrio(objBLL.caidescricao, "{Ofi_Caixa.cai_codigo}=" & objBLL.caicodigo)
        End If
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frm As New RelatoriosfinFRM
            Dim rpt As New CaixaDetalhadoREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frm.CrystalReportViewer2.ShowGroupTreeButton = False
                frm.CrystalReportViewer2.ReportSource = rpt
                frm.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frm.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
End Class
